package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.mybatisplus.utils.AliOssUtil;
import com.atguigu.lease.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@Tag(name = "文件管理")
@RequestMapping("/admin/file")
@RestController
@ComponentScan(value = "com.atguigu.lease.common.mybatisplus")
public class FileUploadController {
    @Autowired
    AliOssUtil aliOssUtil;
    @Operation(summary = "上传文件")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();//获取文件名称
        String[] split = originalFilename.split("\\.");
        String s = split[split.length - 1];//获得文件扩展名
        String string = UUID.randomUUID().toString();//随机获得名称
        String s1 = string +"."+ s;
        String filePath = aliOssUtil.upload(file.getBytes(), s1);//返回文件的网址(返回路径)
        return Result.ok(filePath);
    }

}
